<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <!-- 侦听 -->
      <button @click="obj.name = 'rose'">修改obj.name</button>
      <button @click="obj.age = 99">修改obj.age</button>
      <button @click="obj.pet.nickname = '大黄'">修改obj.pet.nickname</button>
      <p>{{ obj }}</p>
    </div>
    <script src="./vue.js"></script>
    <script>
      new Vue({
        el: '#app',
        data () {
          return {
            obj: {
              name: 'jack',
              age: 16,
              pet: {
                nickname: '旺财',
                age: 1
              }
            }
          }
        },
        watch: {
          // "obj.name" () {
          //   console.log('obj.name改变了')
          // }

          // 如果希望对象里所有属性都能侦听到
          // 甚至是对象里的对象的属性，那就用深度侦听
          obj: {
            // 开启深度侦听
            deep: true,
            handler () {
              // 当obj里任意属性发生就调用handler这个函数
              console.log('obj里有改变了')
            }
          }
        }
      })
    </script>
  </body>
</html>
